package com.atguigu.gulimall.search.thread;


import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ThreadTest {
    public  static ExecutorService executor= Executors.newFixedThreadPool(10);

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        System.out.println("线程开始");
        // 异步线程 runAsync
/*        CompletableFuture.runAsync(()->{
            int i=2<<2;
            System.out.println("当前线程："+Thread.currentThread().getId());
        },executor);*/
        // 有返回值 supplyAsync
/*        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
            int i = 2 << 2;
            int w=1/0;
            System.out.println("当前线程：" + Thread.currentThread().getId());
            return i;
        }, executor).whenComplete((t,u)->{
            // 任务执行成功 拿到异常与返回值 不能进行返回数据
            System.out.println("结果是"+t);
            System.out.println("异常是"+u);
        }).exceptionally(t->{
            // 感知异常 返回自定义结果
            return 10;
        });*/
        // 获取结果
        /*Integer integer = future.get();*/

        // 根据异常进行处理
  /*      CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
            int i = 2 << 2;
            int w=1/0;
            System.out.println("当前线程：" + Thread.currentThread().getId());
            return i;
        }, executor).handle((t,u)->{
            if(t!=null){
                return t;
            }
            if(u!=null){
                return  0;
            }
            return 0;
        });
        future.get();
        */

         // 线程串行化
        /**
         * thenRun:不能获取到上一步的执行
         *    .thenRunAsync(() -> {
         *             System.out.println("任务2执行");
         *         }, executor);
         *
         *  thenAcceptAsync :获取上一步的结果
         *  .thenAcceptAsync(res->{
         *             System.out.println("上一步的结果+"+res);
         *         });
         * thenApplyAsync 拿到上一步结果并且有返回值
         *         thenApplyAsync(
         *                 t -> {
         *                     System.out.println("上一步的结构" + t);
         *                     return t * 2;
         *                 }, executor);
         */
/*        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
            int i = 2 << 2;
            System.out.println("当前线程：" + Thread.currentThread().getId());
            return i;
        }, executor).thenApplyAsync(
                t -> {
                    System.out.println("上一步的结构" + t);
                    return t * 2;
                }, executor);*/
/*        CompletableFuture<Object> future1 = CompletableFuture.supplyAsync(() -> {
            int i = 2 << 2;
  *//*          try {
                Thread.sleep(3000);
                System.out.println("任务1线程结束" );
            } catch (InterruptedException e) {
                e.printStackTrace();
            }*//*
            System.out.println("任务一线程：" + Thread.currentThread().getId());
            return i;
        }, executor);
        CompletableFuture<Object> future2 = CompletableFuture.supplyAsync(
                () -> {
                    System.out.println("任务二线程：" + Thread.currentThread().getId());
       *//*             try {
                        Thread.sleep(3000);
                        System.out.println("任务二线程结束" );
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }*//*
                    return "hellow";
                }, executor);*/
        // 线程池合并 不能感知两个线程的返回值
        /*future1.runAfterBothAsync(future2,()->{
            System.out.println("任务三");
        },executor);*/
        // 感知之前线程的合并 获取返回结果
/*        future1.thenAcceptBothAsync(future2,(f1,f2)->{
            System.out.println("任务一二的返回结果"+f1+"||||"+f2);
        },executor);*/
        // 感知之前线程的合并 获取返回结果 并且有返回值
/*        CompletableFuture<String> future3 = future1.thenCombineAsync(future2, (f1, f2) -> {
            return f1 + "||" + f2 + "->heng";
        }, executor);
        System.out.println("任务三的返回结果"+future3.get());*/
        /**
         * 两个任务只要有一个完成
         */
        // 不感知结果 无返回值 runAfterEitherAsync
     /*   future1.runAfterEitherAsync(future2,()->{
            System.out.println("任务三开始之前的结果");
        },executor);*/
        // 接受感知 无返回值 acceptEitherAsync
/*        future1.acceptEitherAsync(future2,(res)->{
            System.out.println("任务三开始之前的结果"+res);
        },executor);*/
        // 接收感知 有返回值
/*        CompletableFuture<String> future3 = future1.applyToEitherAsync(future2, res -> {
            System.out.println("任务三开始之前的结果" + res);
            return res.toString() + "demo";
        }, executor);
        System.out.println("任务三的返回结果"+future3.get());*/
        // 多任务组合
        CompletableFuture<String> futureImg = CompletableFuture.supplyAsync(() -> {
            System.out.println("查询商品的图片信息");
            return "hello.jpg";
        },executor);
        CompletableFuture<String> futureAttr = CompletableFuture.supplyAsync(() -> {
            System.out.println("查询商品的图片属性");
            return "褐色+25888G";
        },executor);
        CompletableFuture<String> futureDesc= CompletableFuture.supplyAsync(() -> {
            System.out.println("查询商品的图片介绍");
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "华为";
        },executor);
        //等待所有结果完成
        /*CompletableFuture<Void> allOf = CompletableFuture.allOf(futureImg, futureAttr, futureDesc);
        allOf.join();*/
        // 一个成功就执行
        CompletableFuture<Object> anyOf = CompletableFuture.anyOf(futureImg, futureAttr, futureDesc);
        anyOf.get();
        System.out.println("线程结束"+anyOf.get());
    }
}
